package com.ebelter.bodyfatscale.moudules.db;


import android.text.TextUtils;

import com.ebelter.bodyfatscale.moudules.db.beans.HistoryMesureData;
import com.ebelter.bodyfatscale.moudules.db.beans.HistoryMesureDataAvg;
import com.ebelter.bodyfatscale.moudules.db.beans.HistoryMesureDataAvg_Table;
import com.ebelter.bodyfatscale.moudules.db.beans.HistoryMesureData_Table;
import com.ebelter.bodyfatscale.moudules.db.beans.UsersData;
import com.ebelter.bodyfatscale.moudules.db.beans.UsersData_Table;
import com.ebelter.bodyfatscale.util.BaseHandle;
import com.ebelter.bodyfatscale.util.MD5Utils;
import com.ebelter.bodyfatscale.util.NumUtils;
import com.ebelter.bodyfatscale.util.ThreadManager;
import com.ebelter.btlibrary.util.ULog;
import com.raizlabs.android.dbflow.sql.language.Select;

import java.util.List;

public class Daos extends BaseHandle{


    private static final String TAG = "Daos";
    private static Daos instance;
    private BaseHandle mMainHandle;


    public static Daos getInstance() {
        if (instance == null) {
            synchronized (Daos.class) {
                instance = new Daos();
            }
        }
        return instance;
    }

    private Daos() {
        this.mMainHandle = new BaseHandle(new Object());
    }

    //查询是否存在
    public boolean isExistence(String name) {
        UsersData usersData = new Select().from(UsersData.class).where(UsersData_Table.name.eq(name)).querySingle();
        return usersData != null;
    }

    public UsersData getUser(String name) {
        UsersData usersData = new Select().from(UsersData.class).where(UsersData_Table.name.eq(name)).querySingle();
        return usersData;
    }

    public long getLastUserid() {
        List<UsersData> dbList = new Select().from(UsersData.class)
                .orderBy(UsersData_Table.userID, true).queryList();
        if (dbList.size() == 0) {
            return 10000000000L;
        }
        return dbList.get(dbList.size() - 1).userID;
    }


    public UsersData getUsersData(String name, String psw) {
        UsersData usersData = new Select().from(UsersData.class).where(UsersData_Table.name.eq(name)).and(UsersData_Table.psw.eq(MD5Utils.getMd5(psw))).querySingle();
        return usersData;
    }

    public UsersData getUsersDataFromUserId(long userid) {
        UsersData usersData = new Select().from(UsersData.class).where(UsersData_Table.userID.eq(userid)).querySingle();
        return usersData;
    }


    public void printlnHisData(long userid) {
        List<HistoryMesureData> datas = new Select().from(HistoryMesureData.class).where(HistoryMesureData_Table.userID.eq(userid)).queryList();

        ULog.i(TAG, datas.toString());


    }


    //根据用户id查询测量数据 是否按照时间排序
    public List<HistoryMesureData> getUserDatas(long userid, boolean ascending) {
        List<HistoryMesureData> datas;
        if (ascending) {
            datas = new Select().from(HistoryMesureData.class).where(HistoryMesureData_Table.userID.eq(userid)).orderBy(HistoryMesureData_Table.measureTime, true).queryList();
        } else {
            datas = new Select().from(HistoryMesureData.class).where(HistoryMesureData_Table.userID.eq(userid)).orderBy(HistoryMesureData_Table.measureTime, false).queryList();
        }
        return datas;
    }

    public List<HistoryMesureDataAvg> getUserAvgDatas(long userid, boolean ascending) {
        List<HistoryMesureDataAvg> datas;
        if (ascending) {
            datas = new Select().from(HistoryMesureDataAvg.class).where(HistoryMesureDataAvg_Table.userID.eq(userid)).orderBy(HistoryMesureDataAvg_Table.measureTime, true).queryList();
        } else {
            datas = new Select().from(HistoryMesureDataAvg.class).where(HistoryMesureDataAvg_Table.userID.eq(userid)).orderBy(HistoryMesureDataAvg_Table.measureTime, false).queryList();
        }
        return datas;
    }


    //给数据库里面保存一条数据
    //2018-03-15 12:11:12
    public void computeTodayData(final long userId, final String testTime,final DaoInterface.Calculate calculate) {

        if (userId < 0 || TextUtils.isEmpty(testTime)||calculate==null) {
            return;
        }


        ThreadManager.getShortPool().execute(new Runnable() {
            @Override
            public void run() {

                String date = testTime.substring(0, 10);

                ULog.i(TAG, "date = " + date);
                List<HistoryMesureData> todayDatas = new Select().from(HistoryMesureData.class).where(HistoryMesureData_Table.userID.eq(userId)).and(HistoryMesureData_Table.measureTime.like(date+"_________")).queryList();

                ULog.i(TAG, todayDatas.toString());

                HistoryMesureDataAvg avgD = new Select().from(HistoryMesureDataAvg.class).where(HistoryMesureDataAvg_Table.userID.eq(userId)).and(HistoryMesureDataAvg_Table.measureTime.eq(date)).querySingle();




                            //体脂率
                float fat = 0;
                            //体重
                float weight = 0;
                            //肌肉量
                float muscleVolume=0;



                for (HistoryMesureData d : todayDatas) {
                    fat+=d.fat;
                    weight+=d.weight;
                    muscleVolume+=d.muscleVolume;
                }
                int size = todayDatas.size();
                if (size > 0){
                    fat = fat/size;
                    weight = weight/size;
                    muscleVolume = muscleVolume/size;
                }

                if (avgD == null) {
                    avgD = new HistoryMesureDataAvg();
                    avgD.userID = userId;
                    avgD.measureTime = date;
                    avgD.fat= NumUtils.numBaoLiuWei(fat,2);
                    avgD.weight= NumUtils.numBaoLiuWei(weight,2);
                    avgD.muscleVolume= NumUtils.numBaoLiuWei(muscleVolume,2);
                    avgD.save();
                }else {
                    avgD.fat= NumUtils.numBaoLiuWei(fat,2);
                    avgD.weight= NumUtils.numBaoLiuWei(weight,2);
                    avgD.muscleVolume= NumUtils.numBaoLiuWei(muscleVolume,2);
                    avgD.update();
                }

                mMainHandle.post(new Runnable() {
                    @Override
                    public void run() {
                        calculate.Complete();
                    }
                });



            }
        });


    }



}
